<?php
namespace App\Http\Controllers\SystemManagement;

use App\Http\Controllers\Controller;
use App\Models\SystemManagement\UserRole;
use Illuminate\Http\Request;

/**
 * Class UserRoleController
 * @package App\Http\Controllers\SystemManagement
 * @group 用户角色管理
 */
class UserRoleController extends Controller{

    /**
     * 用户分配角色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam user_id int required 用户ID 外键 用户表
     * @bodyParam role_id string required 角色ID 外键 角色表
     *
     * @response 201 {"msg":"created","code":201,"data":"\u65b0\u589e\u6210\u529f"}
     * @response 400 {"msg":"param error","code":400,"data":"\u53c2\u6570\u9519\u8bef"}
     */
    public function roleToUser(\App\Http\Requests\System\UserRole $request){
        if(count($request->input())==0 || is_null($request->input('user_id')) ||
            is_null($request->input('role_id'))){
            return response()->json(encodeMsg("参数错误",400));
        }
        try{
            $ur = new UserRole();
            $ur->create($request->input());
            return response()->json(encodeMsg("新增成功",201));
        }catch (\Exception $e){
            //dd($e->getMessage());
            return response()->json(encodeMsg("新增失败",400));
        }
    }
}
